package com.glodon.paas.document.service.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.glodon.paas.consts.DatabaseConst;
import com.glodon.paas.document.api.bean.File;

/**
 * 文件排序类
 * 
 * @author a
 */
public class OrderUtilsFile {

    // 对File排序工具类
    private static Map<String, Comparator<File>> fileOrderMap = new HashMap<String, Comparator<File>>();
    static {
        fileOrderMap.put("fileName", new NameComparator()); // 文件名
        fileOrderMap.put("extension", new ExtensionComparator()); // 扩展名
        fileOrderMap.put("createTime", new CreateTimeComparator()); // 创建时间
        fileOrderMap.put("updateTime", new UpdateTimeComparator()); // 创建时间
        fileOrderMap.put("size", new SizeComparator()); //
        fileOrderMap.put("fileSize", new SizeComparator()); //
        fileOrderMap.put("fileType", new ExtensionComparator()); //
        fileOrderMap.put("fileOwner", new OwnerComparator()); // 文件所有者
        // fileOrderMap.put("type",new TypeComparator()); //
    };

    /**
     * 不支持的排序，不会排序
     * 
     * @param list
     * @param orderBy
     * @param orderType
     * @return
     */
    public static List<File> order(List<File> list, String orderBy, String orderType) {
        if(list == null || list.size() == 0){
            return list;
        }
        if ("1".equals(orderType)) {
            orderType = "asc";
        }
        if ("-1".equals(orderType)) {
            orderType = "desc";
        }
        Comparator<File> comparator = (Comparator<File>) fileOrderMap.get(orderBy);
        if (comparator == null) {
            return list;
        }

        Collections.sort(list, comparator);

        if (DatabaseConst.ORDER_BY_DESC.equals(orderType)) {
            Collections.reverse(list);
        }

        return list;
    }

    private static class NameComparator implements Comparator<File> {

        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }
            return o1.getName().compareTo(o2.getName());
        }
    }

    private static class ExtensionComparator implements Comparator<File> {

        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }
            return o1.getExtension().compareTo(o2.getExtension());
        }
    }

    private static class CreateTimeComparator implements Comparator<File> {

        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }
            return o1.getCreateTime().compareTo(o2.getCreateTime());
        }
    }

    private static class UpdateTimeComparator implements Comparator<File> {

        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }
            return -o1.getUpdateTime().compareTo(o2.getUpdateTime());
        }
    }

    private static class SizeComparator implements Comparator<File> {

        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }
            return o1.getSize().compareTo(o2.getSize());
        }
    }

    private static class OwnerComparator implements Comparator<File> {

        // 根据ownerName排序
        @Override
        public int compare(File o1, File o2) {
            if (o1.isFolder() && !o2.isFolder()) {
                return -1;
            }

            if (!o1.isFolder() && o2.isFolder()) {
                return 1;
            }

            if (o1.getOwner() == null || o1.getOwner().getDisplayName() == null) {
                return -1;
            }
            if (o2.getOwner() == null || o2.getOwner().getDisplayName() == null) {
                return 1;
            }

            return o1.getOwner().getDisplayName().compareTo(o2.getOwner().getDisplayName());
        }
    }
}
